package com.ecarx.vhaltests.hy11.testcases.climate;


import android.car.VehicleAreaType;
import android.util.Log;

import com.ecarx.vhaltests.IpcpClient;
import com.ecarx.vhaltests.VehicleConstants;
import com.ecarx.vhaltests.VhalUnitTestBase2;
import com.ecarx.vhaltests.hy11.testcases.Utils.EcuUtil;
import com.ecarx.xui.adaptapi.car.IWrapper;
import com.ecarx.xui.adaptapi.car.property.IPropertyValue;
import com.ecarx.xui.adaptapi.car.vehicle.IADAS;

import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.FixMethodOrder;
import org.junit.Test;
import org.junit.runners.MethodSorters;

import java.lang.reflect.Array;
import java.util.Arrays;
import java.util.stream.IntStream;

import vendor.ecarx.xma.automotive.vehicle.V2_0.ECarXVehicleProperty;

@FixMethodOrder(MethodSorters.NAME_ASCENDING)
public class HvacFuncAirFragranceAvailableTimeTest extends VhalUnitTestBase2 {
    @BeforeClass
    public static void testInit() {
        setConfig();
    }

    @Override
    protected int getPropertyId() {
        return ECarXVehicleProperty.HVAC_FUNC_AIR_FRAGRANCE_AVAILABLE_TIME;
    }

    @Override
    protected int getArea() {
        return VehicleAreaType.VEHICLE_AREA_TYPE_GLOBAL;
    }

    @Override
    protected String getValueType() {
        return TYPE_VALUE_INT;
    }

    @Test
    public void test01_Active() {
        mockActive();
        execTestAvailable(true);
    }

    @Test
    public void test02_SlotAvailableTimes() {
        int[] allSlotAvailableTimes = {0,1,3,5,2};
        mockAvailableTime(VehicleConstants.CAR_MODE_NOMAL, VehicleConstants.USG_MODE_DRIVING,allSlotAvailableTimes);
        delay();
        Log.d(TAG,"test02_SlotAvailableTimes allSlotAvailableTimes: "+Arrays.toString(allSlotAvailableTimes));
        int[] results = getCarPropertyMgr().getIntArrayProperty(getPropertyId(),getArea());
        Log.d(TAG, "test02_SlotAvailableTimes results: "+Arrays.toString(results));
        Assert.assertEquals(results.length, results.length);
        Assert.assertArrayEquals(allSlotAvailableTimes,results);
    }

    @Test
    public void test03_NotActive() {
        mockNotActive();
        execTestAvailable(false);
    }

    @Test
    public void test04_NotAvailable() {
        mockNotAvailable();
        execTestAvailable(false);
    }



    public void mockAvailableTime(int carMode, int usgMode,int[] allSlotAvailableTimes) {
        EcuUtil.setCarConfig(getIpcpClient(), new int[]{505}, new int[]{2});

        String[] cemOpIds = {"vehModMngtGlbSafe1CarModSts1", "vehModMngtGlbSafe1UsgModSts",
                "airFragCh1AvlTi", "airFragCh2AvlTi", "airFragCh3AvlTi", "airFragCh4AvlTi", "airFragCh5AvlTi"};
        int[] values = {carMode, usgMode};
        values = IntStream.concat(Arrays.stream(values),
                Arrays.stream(allSlotAvailableTimes)).toArray();
        Log.d(TAG, "mockAvailableTime values: "+Arrays.toString(values));
        EcuUtil.setCEM(getIpcpClient(), cemOpIds, values);
    }

    private void mockActive() {
        EcuUtil.setCarConfig(getIpcpClient(), new int[]{505}, new int[]{2});
        setCarAndUsgMode(VehicleConstants.CAR_MODE_NOMAL, VehicleConstants.USG_MODE_DRIVING);
    }

    private void mockNotActive() {
        EcuUtil.setCarConfig(getIpcpClient(), new int[]{505}, new int[]{2});
        setCarAndUsgMode(VehicleConstants.CAR_MODE_NOMAL, VehicleConstants.USG_MODE_ABANDON);
    }


    private void mockNotAvailable() {
        EcuUtil.setCarConfig(getIpcpClient(), new int[]{505}, new int[]{3});
    }

    private static void setConfig() {
        EcuUtil.registerConfigCarConfig(getIpcpClient());
        EcuUtil.registerConfigCEM(getIpcpClient());
    }


    private void setCarAndUsgMode(int carMode, int usgMode) {
        Log.d(TAG,"setCarAndUsgMode carMode :"+carMode +" usgMode:"+usgMode);
        String[] requests = {"vehModMngtGlbSafe1CarModSts1", "vehModMngtGlbSafe1UsgModSts"};
        int[] values = {carMode, usgMode};
        EcuUtil.setCEM(getIpcpClient(), requests, values);
    }

}
